package com.lhh.springcloud.controller;

import com.lhh.entity.Member;
import com.lhh.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author 李洪豪
 * @Sno 20220301044
 */
@RestController
@Slf4j
public class MemberConsumerNacosApplication {
    //nacos底层默认支持ribbon负载均衡，默认使用的是轮询算法，也可以自己指定
    private static final String MEMBER_SERVICE_NACOS_PROVIDER_URL="http://vipmember-service-nacos-provider";
    @Resource
    private RestTemplate restTemplate;

    @PostMapping("/member/nacos/consumer/save")
    //@RequestBody注解标识请求的数据格式为json，若没有该注解，则请求时需要以表单的方式，否则封装不成功
    public Result<Member> saveMember(@RequestBody Member member) {
        Result result = restTemplate.postForObject(MEMBER_SERVICE_NACOS_PROVIDER_URL + "/member/save", member, Result.class);
        return result;
    }

    @GetMapping("/member/nacos/consumer/query/{id}")
    public Result<Member> queryMemberById(@PathVariable(value = "id") Long id) {
        Result result = restTemplate.getForObject(MEMBER_SERVICE_NACOS_PROVIDER_URL + "/member/query/" + id, Result.class);
        //restTemplate接收到的类型(Result)是由接收方返回的数据类型决定的
        return result;
    }
}
